<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="drag" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all
          :data="list"
          :props="{children:'children',label:'name'}"
        >
          <!-- 使用作用域插槽 获取内部的数据 可以使用提供的默认作用域插槽 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="drag" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="Eadd(data.id)">添加编辑部门</el-dropdown-item>
                        <!-- 二级部门删除才能显示 删除 -->
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hdel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit?'编辑':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @click="hclose"
      >
        <!--方法一 v-if="showDialog"  解决数据不跟新问题 -->
        <addorEdit
          v-if="showDialog"
          :id="curid"
          ref="myadd"
          :origin-list="originList"
          :is-edit="isEdit"
          @success="hsuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
import addorEdit from './deptDialog.vue'
export default {
  components: {
    addorEdit
  },
  data() {
    return {
    // 依赖一份树形数据
      list: [
      ],
      showDialog: false, // 弹层
      curid: '', // 点击部门的id
      isEdit: false, // false 新增 || true 编辑
      originList: []
    }
  },
  created() {
    // 调用加载部门列表
    this.loaddepartment()
  },
  methods: {
    hclose() {
    //   //重置表单 清空检验信息 ---
      this.$refs.myadd.resetForm()
    },
    hsuccess() {
      // 重新渲染页面
      this.loaddepartment()
      // 关闭弹层
      this.showDialog = false
    },
    // 删除部门
    hdel(id) {
      this.$confirm('你确定要删除吗？').then(async() => {
        const res = await delDepartments(id)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 渲染部门列表
        this.loaddepartment()
      }).catch(err => err)
    },
    // 编辑
    Eadd(id) {
      this.curid = id
      this.showDialog = true
      // 方法二 解决组件数据不更新的方式二
      // 可以在父组件调用子组件中的部门详细的数据填充到表单
      // this.$nextTick(() => {
      //   this.refs.myadd.loadDepartmentsDetails()
      // })
      this.isEdit = true
    },
    // 添加
    hadd(id) {
      this.isEdit = false
      this.curid = id
      this.showDialog = true
    },
    // 加载部门列表
    async loaddepartment() {
      try {
        // 发送请求
        const res = await getDepartments()
        this.originList = res.data.depts.map(item => {
          return { name: item.name, pid: item.pid, id: item.id, code: item.code }
        })
        console.log(this.originList, 'this.originList')
        console.log(res)
        // 删除第一个部门名
        res.data.depts.shift()

        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
